﻿using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    public class PaymentDAL
    {
        private Database database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        /// <summary>
        /// 增加付款记录
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="payType"></param>
        /// <param name="payDate"></param>
        /// <param name="amount"></param>
        /// <param name="remark"></param>
        public void AddPaymentRecord(string orderID, int payType, DateTime payDate, int amount,int bank,string remark)
        {
            const string sql = @"insert into Payment (OrderID,PayType,PayDate,Amount,Bank,Remark) values 
                (@OrderID,@PayType,@PayDate,@Amount,@Bank,@Remark)";


            using (var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);
                database.AddInParameter(cmd, "@PayType", DbType.Int32, payType);
                database.AddInParameter(cmd, "@PayDate", DbType.DateTime, payDate);
                database.AddInParameter(cmd, "@Bank", DbType.Int32, bank);
                database.AddInParameter(cmd, "@Amount", DbType.Int32, amount);
                database.AddInParameter(cmd, "@Remark", DbType.String, remark);

                database.ExecuteNonQuery(cmd);
            }
        }

        /// <summary>
        /// 是否已付首款
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public bool HasPayFirst(string orderID)
        {
            const string sql = "select count(*) from payment where OrderID=@OrderID and PayType=1";

            using (var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);

                return Convert.ToInt32(database.ExecuteScalar(cmd)) > 0;
            }
        }

        /// <summary>
        /// 是否已付尾款
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public bool HasPayOver(string orderID)
        {
            const string sql = "select count(*) from payment where OrderID=@OrderID and PayType=3";

            using (var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);

                return Convert.ToInt32(database.ExecuteScalar(cmd)) > 0;
            }
        }

        /// <summary>
        /// 获取订单的总付款额
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public int SumByOrderID(string orderID)
        {
            const string sql = "Payment_SumByOrderID";

            using (var cmd = database.GetStoredProcCommand(sql))
            {
                database.AddInParameter(cmd, "@OrderID",DbType.String,orderID);

                return Convert.ToInt32(database.ExecuteScalar(cmd));
            }
        }

        /// <summary>
        /// 修改订单状态为文件处理中，同时修改支付状态为支付完成
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="empolyeeID"></param>
        public void FinishPay(string orderID,int empolyeeID)
        {
            const string procedure = "Payment_FinishedPay";

            using (var cmd = database.GetStoredProcCommand(procedure))
            {
                database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);
                database.AddInParameter(cmd, "@EmpolyeeID", DbType.Int32, empolyeeID);

                database.ExecuteNonQuery(cmd);
            }
        }

        /// <summary>
        /// 修改订单状态为开始支付
        /// </summary>
        /// <param name="orderID"></param>
        public void BeginPay(string orderID)
        {
            const string procedure = "Payment_BeginPay";

            using (var cmd = database.GetStoredProcCommand(procedure))
            {
                database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);

                database.ExecuteNonQuery(cmd);
            }
        }

        /// <summary>
        /// 根据订单编号获取交易记录
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public DataTable GetTransactionTrackByOrderID(string orderID)
        {
            const string procedure = "Payment_FindRecordByOrderID";

            using(var cmd =database.GetStoredProcCommand(procedure))
            {
                database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);

                return database.ExecuteDataSet(cmd).Tables[0];
            }
        }
    }
}
